package com.hibase.core.interfacetask.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.common.exception.HibaseException;
import com.hibase.core.interfacetask.entity.MdsInterfaceType;
import com.hibase.core.interfacetask.entity.vo.InterfaceTypeCache;
import com.hibase.core.interfacetask.entity.vo.MdsInterfaceTypeSearchVO;
import com.hibase.core.interfacetask.mapper.MdsInterfaceTypeMapper;
import com.hibase.core.interfacetask.service.MdsInterfaceTypeService;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 接口业务类型表 服务实现类
 * </p>
 *
 * @author zhou_jw
 * @since 2020-08-21
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MdsInterfaceTypeServiceImpl extends BaseServiceImpl<MdsInterfaceTypeMapper, MdsInterfaceType> implements MdsInterfaceTypeService {

    @Autowired
    InterfaceTypeCache interfaceTypeCache;

    /**
     * 查询接口业务类型表列表
     */
    @Override
    public PageInfo<MdsInterfaceType> selectMdsInterfaceTypeList(MdsInterfaceTypeSearchVO searchVO) {
        QueryWrapper<MdsInterfaceType> wrapper = new QueryWrapper<>();

        wrapper.like(StrUtil.isNotBlank(searchVO.getBusinessTypeName()),"business_type_name", searchVO.getBusinessTypeName());
        wrapper.like(StrUtil.isNotBlank(searchVO.getBusinessTypeCode()),"business_type_code", searchVO.getBusinessTypeCode());
        wrapper.eq(StrUtil.isNotBlank(searchVO.getRequestType()),"request_type", searchVO.getRequestType());
        wrapper.orderByDesc("business_type_code");

        if (StrUtil.isNotBlank(searchVO.getKeyWord())){
            wrapper.like("business_type_code",searchVO.getKeyWord());
            wrapper.or().like("business_type_name",searchVO.getKeyWord());
        }

        return super.page(searchVO,wrapper);
    }

    /**
     * 改变接口业务类型表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMdsInterfaceType(MdsInterfaceType entity) {
        //如果是新增，则设置为启用状态
        if (StrUtil.isBlank(entity.getId())){
            entity.setBeActive(true);
        }else {
            //如果是修改则删除缓存中的数据
            interfaceTypeCache.delInterfaceType(entity.getBusinessTypeCode());
        }

        //super.store(entity);
        super.saveOrUpdate(entity);
    }

    /**
     * 查询接口业务类型表详情
     */
    @Override
    public MdsInterfaceType viewMdsInterfaceType(String id) {

        MdsInterfaceType entity = super.getById(id, true);

        return entity;
    }

    /**
     * 启用/禁用
     * @param ids
     * @param recordStatus
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeEventsStatus(List<String> ids, boolean recordStatus) {
        List<MdsInterfaceType> mdsInterfaceTypes = (List<MdsInterfaceType>) super.listByIds(ids);
        List<MdsInterfaceType> list = new ArrayList<>();
        if (CollUtil.isNotEmpty(mdsInterfaceTypes)) {
            for (MdsInterfaceType mdsInterfaceType : mdsInterfaceTypes){
                if (mdsInterfaceType.getBeActive() == recordStatus){
                    throw new HibaseException(mdsInterfaceType.getBusinessTypeName() + "请勿重复启用禁用");
                }
                mdsInterfaceType.setBeActive(recordStatus);
                list.add(mdsInterfaceType);
            }
        }
        super.saveOrUpdateBatch(list);
    }

    /**
     * 删除接口业务类型表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMdsInterfaceType(String id) {

        super.deleteBy(id);
    }
}
